<?php
use app\common\message\SuccessMessage;
use think\Route;
//Route::rule('路由表达式','路由地址','请求类型','路由参数（数组）','变量规则（数组）');

// 后台管理模块的api实现
Route::group('admin', function(){
    Route::post('/login', 'admin/Auth/login'); # 登录生成token令牌
    //以下方法都需要验证token
    Route::get('/logout', 'admin/Auth/logout'); # 退出
    Route::get('/auth/info', 'admin/Auth/info'); # 获取管理员信息
    Route::get('/auth/menu', 'admin/Auth/menu'); # 获取后台权限菜单
    // Admin
    Route::get('/admin', 'admin/Admin/index');
    Route::post('/admin', 'admin/Admin/save');
    Route::get('/admin/:id', 'admin/Admin/read',[],['id'=>'\d+']);
    Route::put('/admin/:id', 'admin/Admin/update',[],['id'=>'\d+']);
    Route::delete('/admin/:id', 'admin/Admin/delete',[],['id'=>'\d+']);
    Route::post('/admin/upload_img', 'admin/Admin/upload');
    Route::post('/admin/upload_imgs', 'admin/Admin/uploads');

    // Permission
    Route::get('/permission', 'admin/Permission/index');
    Route::post('/permission', 'admin/Permission/save');
    Route::get('/permission/:id', 'admin/Permission/read', [], ['id'=>'\d+']);
    Route::put('/permission/:id', 'admin/Permission/update',[],['id'=>'\d+']);
    Route::delete('/permission/:id', 'admin/Permission/delete',[],['id'=>'\d+']);
    Route::post('/permission/upload_img', 'admin/Permission/upload');
    Route::post('/permission/upload_imgs', 'admin/Permission/uploads');

    // Role
    Route::get('/role', 'admin/Role/index');
    Route::post('/role', 'admin/Role/save');
    Route::get('/role/:id', 'admin/Role/read', [], ['id'=>'\d+']);
    Route::put('/role/:id', 'admin/Role/update',[],['id'=>'\d+']);
    Route::delete('/role/:id', 'admin/Role/delete',[],['id'=>'\d+']);
    Route::post('/role/upload_img', 'admin/Role/upload');
    Route::post('/role/upload_imgs', 'admin/Role/uploads');
    // Brand
    Route::get('/brand', 'admin/Brand/index');
    Route::post('/brand', 'admin/Brand/save');
    Route::get('/brand/:id', 'admin/Brand/read', [], ['id'=>'\d+']);
    Route::put('/brand/:id', 'admin/Brand/update',[],['id'=>'\d+']);
    Route::delete('/brand/:id', 'admin/Brand/delete',[],['id'=>'\d+']);
    Route::post('/brand/upload_img', 'admin/Brand/upload');
    Route::post('/brand/upload_imgs', 'admin/Brand/uploads');

    // Banner
    Route::get('/banner', 'admin/Banner/index');
    Route::post('/banner', 'admin/Banner/save');
    Route::get('/banner/:id', 'admin/Banner/read', [], ['id'=>'\d+']);
    Route::put('/banner/:id', 'admin/Banner/update',[],['id'=>'\d+']);
    Route::delete('/banner/:id', 'admin/Banner/delete',[],['id'=>'\d+']);
    Route::post('/banner/upload_img', 'admin/Banner/upload');
    Route::post('/banner/upload_imgs', 'admin/Banner/uploads');
    // Category 完成
    Route::get('/category', 'admin/Category/index');
    Route::post('/category', 'admin/Category/save');
    Route::get('/category/:id', 'admin/Category/read', [], ['id'=>'\d+']);
    Route::put('/category/:id', 'admin/Category/update',[],['id'=>'\d+']);
    Route::delete('/category/:id', 'admin/Category/delete',[],['id'=>'\d+']);
    Route::post('/category/upload_img', 'admin/Category/upload');
    Route::post('/category/upload_imgs', 'admin/Category/uploads');

    // Brand
    Route::get('/spec', 'admin/Spec/index');
    Route::post('/spec', 'admin/Spec/save');
    Route::get('/spec/:id', 'admin/Spec/read', [], ['id'=>'\d+']);
    Route::put('/spec/:id', 'admin/Spec/update',[],['id'=>'\d+']);
    Route::delete('/spec/:id', 'admin/Spec/delete',[],['id'=>'\d+']);


});

// 后台管理模块的mvc实现
Route::group('manage', function () {
    Route::get('/', 'manage/Index/index');
    Route::get('/index', 'manage/Index/index');
    Route::get('/index/index', 'manage/Index/index');
    Route::rule('/login', 'manage/Auth/login', 'GET|POST');
    Route::get('/logout', 'manage/Auth/logout');
    Route::get('/admin/index', 'manage/Admin/index');
    Route::get('/admin/:id', 'manage/Admin/detail', [], ['id' => '\d+']);
    Route::rule('/admin/create', 'manage/Admin/create', 'GET|POST');
    Route::rule('/admin/edit/:id', 'manage/Admin/edit', 'GET|POST', [], ['id' => '\d+']);
    Route::rule('/admin/delete/:id', 'manage/Admin/delete', 'GET|POST', [], ['id' => '\d+']);
    Route::get('/role/index', 'manage/Role/index');
    Route::get('/role/:id', 'manage/Role/detail', [], ['id' => '\d+']);
    Route::rule('/role/create', 'manage/Role/create', 'GET|POST');
    Route::rule('/role/edit/:id', 'manage/Role/edit', 'GET|POST', [], ['id' => '\d+']);
    Route::post('/role/delete/:id', 'manage/Role/delete', [], ['id' => '\d+']);
    Route::get('/permission/index', 'manage/Permission/index');
    Route::get('/permission/:id', 'manage/Permission/detail', [], ['id' => '\d+']);
    Route::rule('/permission/create', 'manage/Permission/create', 'GET|POST');
    Route::rule('/permission/edit/:id', 'manage/Permission/edit', 'GET|POST');
    Route::post('/permission/delete/:id', 'manage/Permission/delete');
//  广告位管理
    Route::get('/banner/index', 'manage/Banner/index');
    Route::get('/banner/:id', 'manage/Banner/detail', [], ['id' => '\d+']);
    Route::rule('/banner/create', 'manage/Banner/create', 'GET|POST');
    Route::rule('/banner/edit/:id', 'manage/Banner/edit', 'GET|POST');
    Route::post('/banner/delete/:id', 'manage/Banner/delete');
//  主题管理
    Route::get('/theme/index', 'manage/Theme/index');
    Route::get('/theme/:id', 'manage/Theme/detail', [], ['id' => '\d+']);
    Route::rule('/theme/create', 'manage/Theme/create', 'GET|POST');
    Route::rule('/theme/edit/:id', 'manage/Theme/edit', 'GET|POST');
    Route::post('/theme/delete/:id', 'manage/Theme/delete');
//  商品分类管理
    Route::get('/category/index', 'manage/Category/index');
    Route::get('/category/:id', 'manage/Category/detail', [], ['id' => '\d+']);
    Route::rule('/category/create', 'manage/Category/create', 'GET|POST');
    Route::rule('/category/edit/:id', 'manage/Category/edit', 'GET|POST');
    Route::post('/category/delete/:id', 'manage/Category/delete');
// 后台通用上传图片接口
    Route::post('/upload_img', 'manage/Base/upload_img');
});

// 前台api模块
Route::group('api/:version', function(){
//    Route::get('/demo', 'api/v2.Demo/index');
    Route::post('/token/user', 'api/:version.Token/getUserToken'); #ok
    Route::post('/token/app', 'api/:version.Token/getAppToken'); #ok
    Route::post('/token/verify', 'api/:version.Token/verifyToken'); #ok

    Route::get('/banner/id/:id', 'api/:version.Banner/getBannerByID', [], ['id' => '\d+']); #ok
    //theme?ids=1,2,3
    Route::get('/theme', 'api/:version.Theme/getSimpleList'); #ok
    Route::get('/theme/:id/detail', 'api/:version.Theme/getComplexOne', [], ['id' => '\d+']); #ok
    Route::get('/category/all', 'api/:version.Category/getAll'); #ok
    Route::get('/category/grid/all', 'api/:version.Category/getGridAll');
    Route::get('/category/:id', 'api/:version.Category/getOne', [], ['id' => '\d+']); #ok
    Route::get('/category/pid/:id', 'api/:version.Category/getChildren', [], ['id' => '\d+']); #ok
    Route::get('/product/by/id/:id/detail', 'api/:version.Product/getDetailByID', [], ['id' => '\d+']); #ok
//    Route::get('/product/by/id/:id/detail', 'api/:version.Demo/getDetailByID', [], ['id' => '\d+']);
    Route::get('/product/by/category/:id', 'api/:version.Product/getListByCategoryID', [], ['id' => '\d+']); #ok
    Route::get('/product/latest', 'api/:version.Product/getLatest'); #ok
    Route::get('/product/all', 'api/:version.Product/getAll'); #ok
    Route::post('/address/create', 'api/:version.Address/create');
    Route::get('/address/:id', 'api/:version.Address/getOne', [], ['id' => '\d+']); #ok
    Route::post('/address/update', 'api/:version.Address/update');
    Route::post('/order', 'api/:version.Order/placeOrder'); #ok
    Route::get('/order/:id', 'api/:version.Order/getDetail', [], ['id'=>'\d+']); #ok
    Route::get('/order/by_user', 'api/:version.Order/getSimpleListByUser'); #ok
    Route::get('/order/all', 'api/:version.Order/getAll');# ok
    Route::post('/pay/pre_order', 'api/:version.Pay/getPreOrder'); #ok
//    Route::post('/pay/notify', 'api/:version.Pay/receivePay');#post方式，不能加?参数 xml格式数据
    // 支付回调接口
    Route::post('/pay/notify', 'task/notify/order');#post方式，不能加?参数 xml格式数据
});

# 根据name获取Banner
# /banner/name/b-1
# b-1 || b-2
Route::get('api/:version/banner/name/:name', 'api/:version.Demo/getBannerByName', [], []);
# 获取一组专题
# /theme/by/names?names=t-1,t-2,t-3,t-4,t-5,t-6
Route::get('api/:version/theme/by/names', 'api/:version.Demo/getThemesByName');
# 获取单个专题的详情（含Spu数据）
# /theme/name/t-1/with_spu
# t-1 t-2 t-3 t-4 t-5 t-6
Route::get('api/:version/theme/name/:name/with_spu', 'api/:version.Demo/getThemeWithSpuByName', [], []);
# 获取活动(不包含优惠券数据)
# /activity/name/a-2
# a-1 || a-2
Route::get('api/:version/activity/name/:name', 'api/:version.Demo/getActivityByName', [], []);
# 获取活动（携带优惠券数据）
# /activity/name/t-4/with_spu
# t-4
Route::get('api/:version/activity/name/:name/with_spu', 'api/:version.Demo/getActivityByNameWithSpu', [], []);
/**
 * 获取某个二级分类的可用优惠券
 * /coupon/by/category/2
 * 2
 */
Route::get('api/:version/coupon/by/category/:cid', 'api/:version.Demo/getCouponsByCategoryID', [], ['cid' => '\d+']);
/** 获取全场通用优惠券
 */
Route::get('api/:version/coupon/whole_store', 'api/:version.Demo/getCouponsInWholeStore');
/**
 * 获取我的优惠券
 * 优惠券有三种状态，分别是：
 * 可使用，值为1
 * 已使用，值为2
 * 已过期，值为3
 * /v1/coupon/myself/by/status/3
 * 3
 */
Route::get('api/:version/coupon/myself/by/status/:status', 'api/:version.Demo/getMyselfCouponByStatus', [], ['status' => '\d+']);
/**
 * 获取我可用的优惠券(带分类数据)
 * 此接口用于下单时检验用户的优惠券是否可以使用，所以需要携带分类数据
 * /coupon/myself/available/with_category
 */
Route::get('api/:version/coupon/myself/available/with_category', 'api/:version.Demo/getCouponAvailableWithCategory');
/**
 * 领取优惠券
 * /coupon/collect/3
 * 3
 */
Route::get('api/:version/coupon/collect/:id', 'api/:version.Demo/getCouponCollect', [], ['id' => '\d+']);

/**
 * 获取六宫格数据
 * /category/grid/all
 */
Route::get('api/:version/category/grid/all', 'api/:version.Demo/getAllGrid');
/**
 * SaleExplain 商品补充说明
 * 获取固定说明
 */
Route::get('api/:version/sale_explain/fixed', 'api/:version.Demo/getFixedSaleExplain');
/**
 * Search 商品搜索
 * 搜索商品的简要信息（不包含详情数据）
 * 该接口支持分页参数
 * /search?q={keyword}&start=0&count=10
 * /search?q=林白
 * 等价于
 * /search?q=林白&start=0&count=10
 */
Route::get('api/:version/search', 'api/:version.Demo/getSearch');
/**
 * 获取Tag标签
 * /tag/type/{type}
 * 目前仅支持获取搜索Tag 搜索Tag类型值为 1
 */
Route::get('api/:version/tag/type/:type', 'api/:version.Demo/getTagByType', [], ['type' => '\d+']);

Route::get('/json', function(){
    $data = [
        ['spec_key'=>'颜色', 'spec_value'=>'银色'],
        ['spec_key'=>'容量', 'spec_value'=>'全网通64GB']
    ];
    return json($data)->send();
});

Route::miss(function() {
    $res = new SuccessMessage(null, 'not found', 404);
    return json($res,404)->send();
});
